﻿using An_Im.Model;
using Microsoft.Office.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PPT = Microsoft.Office.Interop.PowerPoint;

namespace An_Im.Core
{
    static class Initializer
    {
        //public static CommandBarButton mainContextMenuButton; // 动画库文件。
        //public static CommandBarButton stormContextMenuButton; // 动画编辑器界面。
        //public static CommandBarButton commentsContextMenuButton; // 批注编辑。

        private static void LoadContextButton()
        {
            //CommandBars bars = Globals.ThisAddIn.Application.CommandBars;
            //var bar = bars["Frames"];  //获取右键菜单项
            //bar.Reset();
            //var controls = bar.Controls;
            //mainContextMenuButton = (CommandBarButton)controls.Add(MsoControlType.msoControlButton, missing, missing, missing, false);
            //mainContextMenuButton.Picture = Converter.PictureConverter.ImageToIPictureDisp(Properties.Resources.AnYi);
            //mainContextMenuButton.Caption = "访问动画库";
            //mainContextMenuButton.Click += MainContextMenuButton_Click;
            //stormContextMenuButton = (CommandBarButton)controls.Add(MsoControlType.msoControlButton, missing, missing, missing, false);
            //stormContextMenuButton.Picture = Converter.PictureConverter.ImageToIPictureDisp(Properties.Resources.AnYi);
            //stormContextMenuButton.Caption = "开始编辑动画";
            //stormContextMenuButton.Click += StormContextMenuButton_Click;

            //bar = bars["Comment Popup"];  //获取右键菜单项 
            //bar.Reset();
            //controls = bar.Controls;
            //commentsContextMenuButton = (CommandBarButton)controls.Add(MsoControlType.msoControlButton, missing, missing, missing, false);
            //commentsContextMenuButton.Picture = Converter.PictureConverter.ImageToIPictureDisp(Properties.Resources.AnYi);
            //commentsContextMenuButton.Caption = "彩蛋（待补充）";
            //commentsContextMenuButton.Click += CommentsContextMenuButton_Click; ;
        }




        /// <summary>
        /// 整个项目的初始化部分。
        /// </summary>
        public static void Initialize()
        {
            try
            {
                CreateDataFolder();

                CoreManipulator.Initialize();

                Command.CommandManager.Initialize();
                TaskPaneManager.Initialize(Globals.ThisAddIn.CustomTaskPanes);

                PopupWindowManager.Initialize();

                CoreManipulator.LoadExtensionHost();
                Command.CommandManager.LoadExtend();


                CoreManipulator.App.AfterPresentationOpen += App_AfterPresentationOpen;
                CoreManipulator.App.PresentationBeforeClose += App_PresentationBeforeClose;



                GlobalMessageStateUpdater.Initialize(CoreManipulator.App);
                GlobalMessageStateUpdater.StartListen();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }

        private static void App_AfterPresentationOpen(PPT.Presentation Pres)
        {
            // 导入预先设定好的VBA模块。
            var pres = CoreManipulator.App.ActivePresentation;
            var vbproj = VBAManipulator.VBProject;
            PowerV.PowerVFrame.LoadInCustomData(pres, vbproj);
        }

        public static void Close()
        {
            CustomAnimationLib.Save();
            TaskPaneManager.SaveTaskPaneConfig();
            AnimEffect.SaveCustomRenamedAnimEffect();
        }


        private static void App_PresentationBeforeClose(PPT.Presentation pres, ref bool Cancel)
        {
            TaskPaneManager.DeleteTaskPane(pres);
            var vbproj = VBAManipulator.VBProject;
            PowerV.PowerVFrame.SaveCustomData(pres, vbproj.VBComponents);
            // pres.Save();
        }



        //private static void CommentsContextMenuButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
        //{
        //    System.Windows.Forms.MessageBox.Show("这是一个彩蛋，以后补充。");
        //}

        //private static void StormContextMenuButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
        //{
        //    var taskPaneGroup = TaskPaneManager.GetTaskPaneGroup(CoreManipulator.App.ActivePresentation);
        //    taskPaneGroup.AnStormPane.Visible = true;
        //}

        //private static void MainContextMenuButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
        //{
        //    var taskPaneGroup = TaskPaneManager.GetTaskPaneGroup(CoreManipulator.App.ActivePresentation);
        //    taskPaneGroup.AnMainPane.Visible = true;
        //}

        /// <summary>
        /// 开启插件时创建数据文件夹。
        /// </summary>
        private static void CreateDataFolder()
        {
            var hasFolderPath = Directory.Exists(CoreManipulator.FolderPath);
            //CoreManipulator.FolderPath = “CoreManipulator.FolderPath”引发了类型“System.TypeInitializationException”的异常
            var hasCustomAnimationExportPath = Directory.Exists(CoreManipulator.CustomAnimationExportPath);
            var hasAddInStorePath = Directory.Exists(CoreManipulator.AddInStorePath);

            if (!hasFolderPath)
                Directory.CreateDirectory(CoreManipulator.FolderPath);

            if (!hasCustomAnimationExportPath)
                Directory.CreateDirectory(CoreManipulator.CustomAnimationExportPath);

            if (!hasAddInStorePath)
                Directory.CreateDirectory(CoreManipulator.AddInStorePath);
            
        }
    }
}
